<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mousewheelhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_mousewheelhandler.js.html">mousewheelhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This event wrapper will dispatch an event when the user uses
<a name="line17"></a> * the mouse wheel to scroll an element. You can get the direction by checking
<a name="line18"></a> * the deltaX and deltaY properties of the event.
<a name="line19"></a> *
<a name="line20"></a> * This class aims to smooth out inconsistencies between browser platforms with
<a name="line21"></a> * regards to mousewheel events, but we do not cover every possible
<a name="line22"></a> * software/hardware combination out there, some of which occasionally produce
<a name="line23"></a> * very large deltas in mousewheel events. If your application wants to guard
<a name="line24"></a> * against extremely large deltas, use the setMaxDeltaX and setMaxDeltaY APIs
<a name="line25"></a> * to set maximum values that make sense for your application.
<a name="line26"></a> *
<a name="line27"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line28"></a> * @see ../demos/mousewheelhandler.html
<a name="line29"></a> */
<a name="line30"></a>
<a name="line31"></a>goog.provide(&#39;goog.events.MouseWheelEvent&#39;);
<a name="line32"></a>goog.provide(&#39;goog.events.MouseWheelHandler&#39;);
<a name="line33"></a>goog.provide(&#39;goog.events.MouseWheelHandler.EventType&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.require(&#39;goog.events&#39;);
<a name="line36"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line37"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line38"></a>goog.require(&#39;goog.math&#39;);
<a name="line39"></a>goog.require(&#39;goog.style&#39;);
<a name="line40"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * This event handler allows you to catch mouse wheel events in a consistent
<a name="line46"></a> * manner.
<a name="line47"></a> * @param {Element|Document} element The element to listen to the mouse wheel
<a name="line48"></a> *     event on.
<a name="line49"></a> * @param {boolean=} opt_capture Whether to handle the mouse wheel event in
<a name="line50"></a> *     capture phase.
<a name="line51"></a> * @constructor
<a name="line52"></a> * @extends {goog.events.EventTarget}
<a name="line53"></a> */
<a name="line54"></a>goog.events.MouseWheelHandler = function(element, opt_capture) {
<a name="line55"></a>  goog.events.EventTarget.call(this);
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * This is the element that we will listen to the real mouse wheel events on.
<a name="line59"></a>   * @type {Element|Document}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.element_ = element;
<a name="line63"></a>
<a name="line64"></a>  var rtlElement = goog.dom.isElement(this.element_) ?
<a name="line65"></a>      /** @type {Element} */ (this.element_) :
<a name="line66"></a>      (this.element_ ? /** @type {Document} */ (this.element_).body : null);
<a name="line67"></a>
<a name="line68"></a>  /**
<a name="line69"></a>   * True if the element exists and is RTL, false otherwise.
<a name="line70"></a>   * @type {boolean}
<a name="line71"></a>   * @private
<a name="line72"></a>   */
<a name="line73"></a>  this.isRtl_ = !!rtlElement &amp;&amp; goog.style.isRightToLeft(rtlElement);
<a name="line74"></a>
<a name="line75"></a>  var type = goog.userAgent.GECKO ? &#39;DOMMouseScroll&#39; : &#39;mousewheel&#39;;
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * The key returned from the goog.events.listen.
<a name="line79"></a>   * @type {goog.events.Key}
<a name="line80"></a>   * @private
<a name="line81"></a>   */
<a name="line82"></a>  this.listenKey_ = goog.events.listen(this.element_, type, this, opt_capture);
<a name="line83"></a>};
<a name="line84"></a>goog.inherits(goog.events.MouseWheelHandler, goog.events.EventTarget);
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Enum type for the events fired by the mouse wheel handler.
<a name="line89"></a> * @enum {string}
<a name="line90"></a> */
<a name="line91"></a>goog.events.MouseWheelHandler.EventType = {
<a name="line92"></a>  MOUSEWHEEL: &#39;mousewheel&#39;
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Optional maximum magnitude for x delta on each mousewheel event.
<a name="line98"></a> * @type {number|undefined}
<a name="line99"></a> * @private
<a name="line100"></a> */
<a name="line101"></a>goog.events.MouseWheelHandler.prototype.maxDeltaX_;
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Optional maximum magnitude for y delta on each mousewheel event.
<a name="line106"></a> * @type {number|undefined}
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.events.MouseWheelHandler.prototype.maxDeltaY_;
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * @param {number} maxDeltaX Maximum magnitude for x delta on each mousewheel
<a name="line114"></a> *     event. Should be non-negative.
<a name="line115"></a> */
<a name="line116"></a>goog.events.MouseWheelHandler.prototype.setMaxDeltaX = function(maxDeltaX) {
<a name="line117"></a>  this.maxDeltaX_ = maxDeltaX;
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * @param {number} maxDeltaY Maximum magnitude for y delta on each mousewheel
<a name="line123"></a> *     event. Should be non-negative.
<a name="line124"></a> */
<a name="line125"></a>goog.events.MouseWheelHandler.prototype.setMaxDeltaY = function(maxDeltaY) {
<a name="line126"></a>  this.maxDeltaY_ = maxDeltaY;
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Handles the events on the element.
<a name="line132"></a> * @param {goog.events.BrowserEvent} e The underlying browser event.
<a name="line133"></a> */
<a name="line134"></a>goog.events.MouseWheelHandler.prototype.handleEvent = function(e) {
<a name="line135"></a>  var deltaX = 0;
<a name="line136"></a>  var deltaY = 0;
<a name="line137"></a>  var detail = 0;
<a name="line138"></a>  var be = e.getBrowserEvent();
<a name="line139"></a>  if (be.type == &#39;mousewheel&#39;) {
<a name="line140"></a>    var wheelDeltaScaleFactor = 1;
<a name="line141"></a>    if (goog.userAgent.IE ||
<a name="line142"></a>        goog.userAgent.WEBKIT &amp;&amp;
<a name="line143"></a>        (goog.userAgent.WINDOWS || goog.userAgent.isVersion(&#39;532.0&#39;))) {
<a name="line144"></a>      // In IE we get a multiple of 120; we adjust to a multiple of 3 to
<a name="line145"></a>      // represent number of lines scrolled (like Gecko).
<a name="line146"></a>      // Newer versions of Webkit match IE behavior, and WebKit on
<a name="line147"></a>      // Windows also matches IE behavior.
<a name="line148"></a>      // See bug https://bugs.webkit.org/show_bug.cgi?id=24368
<a name="line149"></a>      wheelDeltaScaleFactor = 40;
<a name="line150"></a>    }
<a name="line151"></a>
<a name="line152"></a>    detail = goog.events.MouseWheelHandler.smartScale_(
<a name="line153"></a>        -be.wheelDelta, wheelDeltaScaleFactor);
<a name="line154"></a>    if (goog.isDef(be.wheelDeltaX)) {
<a name="line155"></a>      // Webkit has two properties to indicate directional scroll, and
<a name="line156"></a>      // can scroll both directions at once.
<a name="line157"></a>      deltaX = goog.events.MouseWheelHandler.smartScale_(
<a name="line158"></a>          -be.wheelDeltaX, wheelDeltaScaleFactor);
<a name="line159"></a>      deltaY = goog.events.MouseWheelHandler.smartScale_(
<a name="line160"></a>          -be.wheelDeltaY, wheelDeltaScaleFactor);
<a name="line161"></a>    } else {
<a name="line162"></a>      deltaY = detail;
<a name="line163"></a>    }
<a name="line164"></a>
<a name="line165"></a>    // Historical note: Opera (pre 9.5) used to negate the detail value.
<a name="line166"></a>  } else { // Gecko
<a name="line167"></a>    // Gecko returns multiple of 3 (representing the number of lines scrolled)
<a name="line168"></a>    detail = be.detail;
<a name="line169"></a>
<a name="line170"></a>    // Gecko sometimes returns really big values if the user changes settings to
<a name="line171"></a>    // scroll a whole page per scroll
<a name="line172"></a>    if (detail &gt; 100) {
<a name="line173"></a>      detail = 3;
<a name="line174"></a>    } else if (detail &lt; -100) {
<a name="line175"></a>      detail = -3;
<a name="line176"></a>    }
<a name="line177"></a>
<a name="line178"></a>    // Firefox 3.1 adds an axis field to the event to indicate direction of
<a name="line179"></a>    // scroll.  See https://developer.mozilla.org/en/Gecko-Specific_DOM_Events
<a name="line180"></a>    if (goog.isDef(be.axis) &amp;&amp; be.axis === be.HORIZONTAL_AXIS) {
<a name="line181"></a>      deltaX = detail;
<a name="line182"></a>    } else {
<a name="line183"></a>      deltaY = detail;
<a name="line184"></a>    }
<a name="line185"></a>  }
<a name="line186"></a>
<a name="line187"></a>  if (goog.isNumber(this.maxDeltaX_)) {
<a name="line188"></a>    deltaX = goog.math.clamp(deltaX, -this.maxDeltaX_, this.maxDeltaX_);
<a name="line189"></a>  }
<a name="line190"></a>  if (goog.isNumber(this.maxDeltaY_)) {
<a name="line191"></a>    deltaY = goog.math.clamp(deltaY, -this.maxDeltaY_, this.maxDeltaY_);
<a name="line192"></a>  }
<a name="line193"></a>  // Don&#39;t clamp &#39;detail&#39;, since it could be ambiguous which axis it refers to
<a name="line194"></a>  // and because it&#39;s informally deprecated anyways.
<a name="line195"></a>
<a name="line196"></a>  // For horizontal scrolling we need to flip the value for RTL grids.
<a name="line197"></a>  if (this.isRtl_) {
<a name="line198"></a>    deltaX = -deltaX;
<a name="line199"></a>  }
<a name="line200"></a>  var newEvent = new goog.events.MouseWheelEvent(detail, be, deltaX, deltaY);
<a name="line201"></a>  this.dispatchEvent(newEvent);
<a name="line202"></a>};
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Helper for scaling down a mousewheel delta by a scale factor, if appropriate.
<a name="line207"></a> * @param {number} mouseWheelDelta Delta from a mouse wheel event. Expected to
<a name="line208"></a> *     be an integer.
<a name="line209"></a> * @param {number} scaleFactor Factor to scale the delta down by. Expected to
<a name="line210"></a> *     be an integer.
<a name="line211"></a> * @return {number} Scaled-down delta value, or the original delta if the
<a name="line212"></a> *     scaleFactor does not appear to be applicable.
<a name="line213"></a> * @private
<a name="line214"></a> */
<a name="line215"></a>goog.events.MouseWheelHandler.smartScale_ = function(mouseWheelDelta,
<a name="line216"></a>    scaleFactor) {
<a name="line217"></a>  // The basic problem here is that in Webkit on Mac and Linux, we can get two
<a name="line218"></a>  // very different types of mousewheel events: from continuous devices
<a name="line219"></a>  // (touchpads, Mighty Mouse) or non-continuous devices (normal wheel mice).
<a name="line220"></a>  //
<a name="line221"></a>  // Non-continuous devices in Webkit get their wheel deltas scaled up to
<a name="line222"></a>  // behave like IE. Continuous devices return much smaller unscaled values
<a name="line223"></a>  // (which most of the time will not be cleanly divisible by the IE scale
<a name="line224"></a>  // factor), so we should not try to normalize them down.
<a name="line225"></a>  //
<a name="line226"></a>  // Detailed discussion:
<a name="line227"></a>  //   https://bugs.webkit.org/show_bug.cgi?id=29601
<a name="line228"></a>  //   http://trac.webkit.org/browser/trunk/WebKit/chromium/src/mac/WebInputEventFactory.mm#L1063
<a name="line229"></a>  if (goog.userAgent.WEBKIT &amp;&amp;
<a name="line230"></a>      (goog.userAgent.MAC || goog.userAgent.LINUX) &amp;&amp;
<a name="line231"></a>      (mouseWheelDelta % scaleFactor) != 0) {
<a name="line232"></a>    return mouseWheelDelta;
<a name="line233"></a>  } else {
<a name="line234"></a>    return mouseWheelDelta / scaleFactor;
<a name="line235"></a>  }
<a name="line236"></a>};
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/** @override */
<a name="line240"></a>goog.events.MouseWheelHandler.prototype.disposeInternal = function() {
<a name="line241"></a>  goog.events.MouseWheelHandler.superClass_.disposeInternal.call(this);
<a name="line242"></a>  goog.events.unlistenByKey(this.listenKey_);
<a name="line243"></a>  this.listenKey_ = null;
<a name="line244"></a>};
<a name="line245"></a>
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * A base class for mouse wheel events. This is used with the
<a name="line250"></a> * MouseWheelHandler.
<a name="line251"></a> *
<a name="line252"></a> * @param {number} detail The number of rows the user scrolled.
<a name="line253"></a> * @param {Event} browserEvent Browser event object.
<a name="line254"></a> * @param {number} deltaX The number of rows the user scrolled in the X
<a name="line255"></a> *     direction.
<a name="line256"></a> * @param {number} deltaY The number of rows the user scrolled in the Y
<a name="line257"></a> *     direction.
<a name="line258"></a> * @constructor
<a name="line259"></a> * @extends {goog.events.BrowserEvent}
<a name="line260"></a> */
<a name="line261"></a>goog.events.MouseWheelEvent = function(detail, browserEvent, deltaX, deltaY) {
<a name="line262"></a>  goog.events.BrowserEvent.call(this, browserEvent);
<a name="line263"></a>
<a name="line264"></a>  this.type = goog.events.MouseWheelHandler.EventType.MOUSEWHEEL;
<a name="line265"></a>
<a name="line266"></a>  /**
<a name="line267"></a>   * The number of lines the user scrolled
<a name="line268"></a>   * @type {number}
<a name="line269"></a>   * NOTE: Informally deprecated. Use deltaX and deltaY instead, they provide
<a name="line270"></a>   * more information.
<a name="line271"></a>   */
<a name="line272"></a>  this.detail = detail;
<a name="line273"></a>
<a name="line274"></a>  /**
<a name="line275"></a>   * The number of &quot;lines&quot; scrolled in the X direction.
<a name="line276"></a>   *
<a name="line277"></a>   * Note that not all browsers provide enough information to distinguish
<a name="line278"></a>   * horizontal and vertical scroll events, so for these unsupported browsers,
<a name="line279"></a>   * we will always have a deltaX of 0, even if the user scrolled their mouse
<a name="line280"></a>   * wheel or trackpad sideways.
<a name="line281"></a>   *
<a name="line282"></a>   * Currently supported browsers are Webkit and Firefox 3.1 or later.
<a name="line283"></a>   *
<a name="line284"></a>   * @type {number}
<a name="line285"></a>   */
<a name="line286"></a>  this.deltaX = deltaX;
<a name="line287"></a>
<a name="line288"></a>  /**
<a name="line289"></a>   * The number of lines scrolled in the Y direction.
<a name="line290"></a>   * @type {number}
<a name="line291"></a>   */
<a name="line292"></a>  this.deltaY = deltaY;
<a name="line293"></a>};
<a name="line294"></a>goog.inherits(goog.events.MouseWheelEvent, goog.events.BrowserEvent);
</pre>


</body>
</html>
